import re


def get_image_texts(txt_path):
    with open(txt_path, 'r', encoding='utf-8') as f:
        content = f.read().replace('\n', '')
        return re.split(r'\[image\d+\]', content)[1:]


def get_srt_lines(srt_path):
    with open(srt_path, 'r', encoding='utf-8') as f:
        content = f.read().replace(' ', '')
        segments = re.split(r'\n\n', content)
        lines = [segment.split('\n')[2] for segment in segments if len(
            segment.split('\n')) >= 3]
        return lines


txt_path = "srt_txt/txt.txt"
srt_path = "srt_txt/srt.srt"

image_texts = get_image_texts(txt_path)
srt_lines = get_srt_lines(srt_path)


def milliseconds_to_timestamp(milliseconds):
    total_seconds, mmm = divmod(milliseconds, 1000)
    hh, remainder = divmod(total_seconds, 3600)
    mm, ss = divmod(remainder, 60)
    timestamp = f"{hh:02d}:{mm:02d}:{ss:02d},{mmm:03d}"
    return timestamp


def timestamp_to_milliseconds(timestamp):
    hh, mm, ss_mmm = timestamp.split(':')
    ss, mmm = ss_mmm.split(',')
    milliseconds = int(hh) * 3600000 + int(mm) * \
        60000 + int(ss) * 1000 + int(mmm)
    return milliseconds


def get_total_duration_in_ms(srt_path):
    with open(srt_path, 'r', encoding='utf-8') as f:
        lines = f.readlines()

    for i in range(0, len(lines), 4):
        start_time, end_time = lines[i+1].strip().split(' --> ')
    return timestamp_to_milliseconds(end_time)


total = get_total_duration_in_ms(srt_path)
print("总时长：", total)
zzs = len(srt_lines[0])
print("总字数：", zzs)
pjsc = total/zzs
print("平均时长：", pjsc)

result = []

for tx in image_texts:
    result.append(round(pjsc*len(tx)))


print(result)

print(len(result))

with open("srt_txt/片段时长.txt", "w") as file:
    for item in result:
        file.write(str(item) + "\n")
